package xyz.quanguanjia.masterboy;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.lifecycle.Lifecycle;


import java.util.ArrayList;

import xyz.quanguanjia.masterboy.base.BaseFragment;

public class ViewPagerAdapter extends FragmentStateAdapter {
    private ArrayList<BaseFragment> fragments;

    private FragmentManager fm;

    public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, ArrayList<BaseFragment> fragments, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
        this.fragments = fragments;
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // 根据位置创建相应的 Fragment
        return this.fragments.get(position);
    }

    public View getTabView(int position, int status) {
        View view = LayoutInflater.from(MBDApplication.getContext()).inflate(R.layout.tab_item, null);

        BaseFragment fragment = fragments.get(position);
        String title = fragment.fragmentTitle();
        int icon = fragment.fragmentIconId(status);

        ImageView iconImageView = view.findViewById(R.id.tab_icon);
        iconImageView.setImageResource(icon);

        TextView titleTextView = view.findViewById(R.id.tab_title);
        titleTextView.setText(title);

        return view;
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

}
